﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;

/// <summary>
/// 大复式杀
/// </summary>
public class DuplexSha : DoubleData
{
    public List<ThreadData> _threadDatas;
    public ShaStatues isRunStatus = ShaStatues.Idle;
    public bool isShutDown = false;
    public Action<List<ThreadData>> OnSuccessAction;
    List<Action<ThreadData>> _threads = new List<Action<ThreadData>>();

    public int timesCount = 7000;

    public override void StartShaHao(DoubleInputArgs argsValue, List<List<int>> out_data)
    {
        base.StartShaHao(argsValue, out_data);
        List<List<int>> input_nums_array = (List<List<int>>) argsValue.args;

        var datas = Duplex2SingeNote(input_nums_array);
        if (datas == null)
        {
            return;
        }

        Dictionary<int, List<List<int>>> values = new Dictionary<int, List<List<int>>>();
        var cc_count = Mathf.CeilToInt(out_data.Count / timesCount);

        // 分配每个线程要杀的号
        for (int i = 0; i < cc_count; i++)
        {
            List<List<int>> test = new List<List<int>>();
            var end_max = timesCount * (i + 1);
            if (end_max >= out_data.Count)
            {
                end_max = out_data.Count;
            }

            for (int j = i * timesCount; j < end_max; j++)
            {
                test.Add(out_data[j]);
            }

            values.Add(i, test);
        }

        var temp_data = datas;
        var out_arg = new List<ThreadData>();
        //当前线程要处理的数据

        Action loadAction = () =>
        {
            for (int v_index = 0; v_index < values.Count; v_index++)
            {
                var input_data = values[v_index];
                for (int i = 0; i < input_data.Count; i++)
                {
                    if (isShutDown)
                    {
                        return;
                    }

                    // 输入的单注
                    var value_temp_data = temp_data[i];
                    var index = input_data.FindIndex((data) =>
                    {
                        // 6个数的检查
                        for (int j = 0; j < data.Count; j++)
                        {
                            if (data[j] != value_temp_data[j])
                            {
                                return false;
                            }
                        }

                        return true;
                    });
                    if (index != -1)
                    {
                        input_data.RemoveAt(index);
                    }

//                    Thread.Sleep(5);
                }

                Debug.Log($"线程 {v_index} 剩余{input_data.Count} ...");
//                Thread.Sleep(30);
                out_arg.Add(new ThreadData()
                {
                    name = $"{v_index}",
                    curdata = input_data,
                    input_data = temp_data,
                    isSuccess = true,
                    Index = v_index,
                    shaHaoType = ShaHaoType.Duplex
                });
            }
        };

        loadAction.BeginInvoke((ar) =>
        {
            loadAction.EndInvoke(ar);
            bool isAll = true;

            isRunStatus = ShaStatues.EndSha;
            this._threadDatas = out_arg;
            OnSuccessAction?.Invoke(out_arg);
        }, null);
    }


    public Action<int, string> tipsAction;

    public override void Release()
    {
        base.Release();
        this.isShutDown = true;
    }

    public List<List<int>> Duplex2SingeNote(List<List<int>> input_nums_array)
    {
        var datas = new List<List<int>>();

        #region 大复式组合成单注

        for (int j = 0; j < input_nums_array.Count; j++)
        {
            List<int> input_nums = input_nums_array[j];
            var max = input_nums.Count;
            for (int i = 1; i <= max - 5; i++)
            {
                for (int i2 = i + 1; i2 <= max - 4; i2++)
                {
                    for (int i3 = i2 + 1; i3 <= max - 3; i3++)
                    {
                        for (int i4 = i3 + 1; i4 <= max - 2; i4++)
                        {
                            for (int i5 = i4 + 1; i5 <= max - 1; i5++)
                            {
                                for (int i6 = i5 + 1; i6 <= max; i6++)
                                {
                                    datas.Add(new List<int>()
                                    {
                                        input_nums[i - 1], input_nums[i2 - 1], input_nums[i3 - 1],
                                        input_nums[i4 - 1],
                                        input_nums[i5 - 1],
                                        input_nums[i6 - 1]
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }

        #endregion

        return datas;
    }
}